/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.sx.sx1.lintcode.day717.filetransfor;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import java.io.FileOutputStream;
import java.io.*;

/**
 *
 * @author Administrator
 */
public class FtpTest {

    public static void main(String[] args) {

        //ubuntu22 安裝ftp  https://www.jb51.net/article/158697.htm
        //https://blog.csdn.net/weixin_40903060/article/details/140392555
        try {
            //1. 连接到FTP服务器

            String server = "192.168.100.107";
            int port = 21;
            String user = "anonymous";
            String pass = "password";

            FTPClient ftpClient = new FTPClient();
            ftpClient.connect(server, port);
            ftpClient.login(user, pass);

            //2. 获取FTP服务器上指定目录下的文件列表
            FTPFile[] files = ftpClient.listFiles("/sjjhdir");
            for (FTPFile file : files) {  //file文件名是任务ID
                if (file.isFile()) {
                    System.out.println("File: " + file.getName());
                } else if (file.isDirectory()) {
                    System.out.println("Directory: " + file.getName());

                    String remoteFile = "/sjjhdir/" + file.getName();
                    String localFile = "F:/netbeanxm/ftp/" + file.getName();

                    // 创建一个表示新目录的File对象
                    File newDirectory = new File(localFile);

                    // 判断目录是否存在，如果不存在则尝试创建
                    if (!newDirectory.exists()) {
                        // 创建目录
                        boolean wasSuccessful = newDirectory.mkdir();

                        if (wasSuccessful) {
                            System.out.println("Directory created successfully");
                        } else {
                            System.out.println("Failed to create directory");
                        }
                    } else {
                        System.out.println("Directory already exists");
                        continue;
                    }

                    //创建目录之后，目录名是任务ID,目录下存放一个一个的文件，问题数据文件
                    FTPFile[] subfiles = ftpClient.listFiles(remoteFile);

                    for (FTPFile subfile : subfiles) {   //subfile 是一个个问题数据
                        if (subfile.isFile()) {
                            System.out.println("subFile: " + subfile.getName());

                            String remoteQsFile = "/sjjhdir/" + file.getName() + "/" + subfile.getName();
                            String localQsFile = "F:/netbeanxm/ftp/" + file.getName() + "/" + subfile.getName();
                            FileOutputStream fos = new FileOutputStream(localQsFile);
//
                            ftpClient.retrieveFile(remoteQsFile, fos);
                            fos.close();
                        }
                    }

//                    FileOutputStream fos = new FileOutputStream(localFile);
//                    ftpClient.retrieveFile(remoteFile, fos);
//                    fos.close();
                }
            }
            /*
            Directory: task1319
           Directory: task1320
             */
            //3. 下载文件或文件夹到本地目录
//            String remoteFile = "/remote/file.txt";
//            String localFile = "localfile.txt";
//            FileOutputStream fos = new FileOutputStream(localFile);
//
//            ftpClient.retrieveFile(remoteFile, fos);
//            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
